package com.xfreelance.qcshop.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.xfreelance.qcshop.configure.WxConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WxUtil {
    @Autowired
    private WxConfig wxConfig;

    public String getOpenId(String code) {
        String response = HttpRequest.get(wxConfig.getUrl() + "?appid="+ wxConfig.getAppid() +
                "&secret=" + wxConfig.getAppsecret() + "&js_code=" + code + "&grant_type=authorization_code")
                .execute().body();

        return JSONUtil.parseObj(response).getStr("openid");
    }
}
